<?php

defined('BASEPATH') or die('forbidden');

class Cache_Controller Extends Controller {

    var $filearr = array();
    var $filemd5 = array();

    function _init() {
        check_level(50, segment(0), segment(1));
        $this->set_file_array();
    }

    function index() {
        $this->view->assign("type", $this->cache->type == 'apc' ? translate('GENERAL_CACHE_APC') :  translate('GENERAL_CACHE_FILE') );
        set_area_list_view('arealist');
        $cachelist = $this->form->select("cachelist", $this->filearr, array(), array('multiple' => 'multiple', 'class' => 'multiselect', 'id' => 'cacheIds'));
        $this->view->assign("cachelist", $cachelist);
        $this->view->display("general/cache/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        if ($this->cache->type == 'apc') {
            $apccache = apc_cache_info("user");
            foreach ($apccache['cache_list'] as $m => $n) {
                //排除不属于当于分区的缓存
                if ($this->config['area_id'] && !in_array($n['info'], $this->cache->publiccache) && substr($n['info'], 0, strlen($this->config['area_id'])) != $this->config['area_id']) {
                    continue;
                }
                $name = str_replace(array($this->config['area_id']), '', $n['info']);
                $name = isset($this->filearr[$name]) ? $this->filearr[$name] : $name;
                $fileinfo[] = array('id' => $n['info'], 'name' => $name, 'size' => $n['mem_size'], 'ctime' => $n['creation_time'], 'mtime' => $n['mtime'], 'atime' => $n['access_time']);
            }
        } else {
            $files = scandir($this->cache->dir);
            $fileinfo = array();
            foreach ($files as $file) {
                if ($file == '.' || $file == '..')
                    continue;
                $absfile = $this->cache->dir . '/' . $file;
                $cacheinfo = $this->get_file_name($file);
                $fileinfo[] = array('id' => $cacheinfo['tag'], 'name' => $cacheinfo['name'], 'size' => filesize($absfile), 'ctime' => filectime($absfile), 'mtime' => filemtime($absfile), 'atime' => fileatime($absfile));
            }
        }
        $fileinfo = array_sort($fileinfo, $sortname, "SORT_" . strtoupper($sortorder));
        $rows = array();
        foreach ($fileinfo as $m => $n) {
            $n['size'] = format_filesize($n['size']);
            $n['ctime'] = get_datetime($n['ctime']);
            $n['mtime'] = get_datetime($n['mtime']);
            $n['atime'] = get_datetime($n['atime']);
            $rows[] = array('id' => $n['id'], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => count($rows),
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function get_file_name($file) {
        $md5 = str_replace(array("cache_", CACHE_SUFFIX), '', $file);
        $name = array_key_exists($md5, $this->filemd5) ? $this->filearr[$this->filemd5[$md5]] : $md5;
        $tag = isset($this->filemd5[$md5]) ? $this->filemd5[$md5] : $md5;
        return array('md5' => $md5, 'name' => $name, 'tag' => $tag);
    }

    function multiclear() {
        $this->cache->clear();
        //insert_log( translate('GENERAL_CACHE_CLEAR_ALL'));
        if (is_ajax()) {
            json_output(1, '');
        } else {
            success( translate('C_TIPS_SUCCESS_TEXT'), "javascript:;", 500);
        }
    }

    function areaclear() {
        $areaIds = $this->input->post("areaIds");
        $cacheIds = $this->input->post("cacheIds");
        if ($areaIds) {
            //insert_log( translate('GENERAL_CACHE_SYNC_AREACLEAR',implode(",", $areaIds))  );
            json_output(1, set_async_data($areaIds, translate('GENERAL_CACHE_AREACLEAR'), __CLASS__, __FUNCTION__, array('cacheIds' => $cacheIds), true));
        } else {
            json_output(0, translate('C_SELECT_AREA_TEXT'));
        }
    }

    function del() {
        $c = 0;
        $r = '';
        $id = segment(3, 0);
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));

        if (!$this->cache->get($id))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $this->cache->clear($id);
        $c = 1;
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = explode(',', $ids);
            if ($type == 'delete') {
                foreach ($ids as $m => $n) {
                    $this->cache->clear($n);
                }
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

    function set_file_array() {
        $this->filearr = get_cachename_list();
        foreach ($this->filearr as $m => $n) {
            $this->filemd5[md5($m)] = $m;
        }
    }

}